<?php

namespace App\Console\Commands\Loan;

use App\Models\LoanApplication;
use Illuminate\Console\Command;

class RefreshUnallocated extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'loan:unlocate:refresh';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'refresh unlocated loan to current date';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->comment('Refreshing Unlocated loan `apply_at`...');

        $date = date("Y-m-d H:i:s");
        $count = LoanApplication::where('user_id', 0)
            ->where('source', 1)
            ->where('is_allocated', 0)
            ->where('apply_at', '<', date("Y-m-d H:i:s", strtotime("-1 day")))
            ->update(['apply_at' => $date ]);

        $this->info("Updated {$count} record(s) `apply_at` to `{$date}` .");

        return Command::SUCCESS;
    }
}
